package org.vaadin.golfscoretracker.ui.controller;

import java.util.Date;
import java.util.List;

import org.vaadin.appfoundation.persistence.facade.FacadeFactory;
import org.vaadin.golfscoretracker.data.Round;
import org.vaadin.golfscoretracker.data.RoundSet;

import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.util.IndexedContainer;

public class RoundController implements DataEditorController<RoundSet> {

    @Override
    public boolean addItem(Container container, RoundSet pojo) {
        if (container == null || pojo == null) {
            return false;
        }

        Item item = container.addItem(pojo);
        if (item == null) {
            return false;
        }

        updateItemProperties(container, pojo);

        return true;
    }

    @Override
    public RoundSet createPojo() {
        return new RoundSet();
    }

    @Override
    public Container getContainer() {
        IndexedContainer container = new IndexedContainer();
        container.addContainerProperty("course", String.class, null);
        container.addContainerProperty("date", Date.class, null);
        container.addContainerProperty("players", String.class, null);

        List<RoundSet> rounds = FacadeFactory.getFacade().list(RoundSet.class);
        if (rounds != null) {
            for (RoundSet roundSet : rounds) {
                addItem(container, roundSet);
            }
        }

        return container;
    }

    @Override
    public void remove(RoundSet pojo) {
        FacadeFactory.getFacade().delete(pojo);
    }

    @Override
    public void save(RoundSet pojo) {
        FacadeFactory.getFacade().store(pojo);
    }

    private void setProperties(Item item, RoundSet pojo) {
        if (pojo == null) {
            throw new IllegalArgumentException("Round set may not be null");
        }

        if (item == null) {
            throw new IllegalArgumentException("Item may not be null");
        }

        if (pojo.getCourse() != null) {
            item.getItemProperty("course").setValue(pojo.getCourse().getName());
        }

        item.getItemProperty("date").setValue(pojo.getDate());

        String players = "";
        if (pojo.getRounds() != null) {
            for (Round round : pojo.getRounds()) {
                if (round.getPlayer() == null) {
                    continue;
                }

                players += ", " + round.getPlayer().getName();
            }

            if (players.length() > 2) {
                players = players.substring(2);
            }
        }
        item.getItemProperty("players").setValue(players);
    }

    @Override
    public void updateItemProperties(Container container, RoundSet pojo) {
        if (container == null) {
            throw new IllegalArgumentException("Container may not be null");
        }
        if (pojo == null) {
            throw new IllegalArgumentException("Round set may not be null");
        }

        Item item = container.getItem(pojo);
        setProperties(item, pojo);
    }

}
